package com.genersoft.iot.vmp.storager.dao;

import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用于存储设备通道信息
 */
@Mapper
@Repository
public interface DeviceChannelMapper {

    @Insert(value = {" <script> INSERT INTO device_channel (channel_id, device_id, name, manufacture, model, owner, civil_code, block, " +
            "address, parental, parent_id, safety_way, register_way, cert_num, certifiable, err_code, secrecy, " +
            "ip_address, port, password, ptz_type, status, stream_id, longitude, latitude, create_time, update_time,has_audio) " +
            "VALUES ('${channelId}', '${deviceId}', '${name}', '${manufacture}', '${model}', '${owner}', '${civilCode}', '${block}'," +
            "'${address}', ${parental}, '${parentId}', ${safetyWay}, ${registerWay}, '${certNum}', ${certifiable}, ${errCode}, '${secrecy}', " +
            "'${ipAddress}', ${port}, '${password}', ${PTZType}, ${status}, '${streamId}', ${longitude}, ${latitude},'${createTime}', '${updateTime}',"+
            " <if test=\"hasAudio == true\" >  1 </if>" +
            " <if test=\"hasAudio == false\" >  0 </if>" +
    		") </script>"})
    int add(DeviceChannel channel);

    @Update(value = {" <script>" +
            "UPDATE device_channel " +
            "SET update_time='${updateTime}'" +
            "<if test=\"name != null\">, name='${name}'</if>" +
            "<if test=\"manufacture != null\">, manufacture='${manufacture}'</if>" +
            "<if test=\"model != null\">, model='${model}'</if>" +
            "<if test=\"owner != null\">, owner='${owner}'</if>" +
            "<if test=\"civilCode != null\">, civil_code='${civilCode}'</if>" +
            "<if test=\"block != null\">, block='${block}'</if>" +
            "<if test=\"address != null\">, address='${address}'</if>" +
            "<if test=\"parental != null\">, parental=${parental}</if>" +
            "<if test=\"parentId != null\">, parent_id='${parentId}'</if>" +
            "<if test=\"safetyWay != null\">, safety_way=${safetyWay}</if>" +
            "<if test=\"registerWay != null\">, register_way=${registerWay}</if>" +
            "<if test=\"certNum != null\">, cert_num='${certNum}'</if>" +
            "<if test=\"certifiable != null\">, certifiable=${certifiable}</if>" +
            "<if test=\"errCode != null\">, err_code=${errCode}</if>" +
            "<if test=\"secrecy != null\">, secrecy='${secrecy}'</if>" +
            "<if test=\"ipAddress != null\">, ip_address='${ipAddress}'</if>" +
            "<if test=\"port != null\">, port=${port}</if>" +
            "<if test=\"password != null\">, password='${password}'</if>" +
            "<if test=\"PTZType != null\">, ptz_type=${PTZType}</if>" +
            "<if test=\"status != null\">, status='${status}'</if>" +
            "<if test=\"streamId != null\">, stream_id='${streamId}'</if>" +
            " <if test=\"hasAudio != null and hasAudio == true\" >  , has_audio=1</if>" +
            " <if test=\"hasAudio != null and hasAudio == false\" >  , has_audio=0</if>" +
            "<if test=\"longitude != null\">, longitude=${longitude}</if>" +
            "<if test=\"latitude != null\">, latitude=${latitude}</if>" +
            "WHERE deviceId='${deviceId}' AND channel_id='${channelId}'"+
            " </script>"})
    int update(DeviceChannel channel);

    @Select(value = {" <script>" +
            "SELECT * FROM ( "+
            " SELECT dc.* , (SELECT count(0) FROM device_channel WHERE parent_id=dc.channel_id) as subCount FROM device_channel dc " +
            " WHERE dc.device_id=#{deviceId} " +
            " <if test=\"query != null\"> AND (dc.channel_id LIKE '%${query}%' OR dc.name LIKE '%${query}%' OR dc.name LIKE '%${query}%')</if> " +
            " <if test=\"parentChannelId != null\"> AND dc.parent_id=#{parentChannelId} </if> " +
            " <if test=\"online != null and online == true\" > AND dc.status=1</if>" +
            " <if test=\"online != null and online == false\" > AND dc.status=0</if>) dcr" +
            " WHERE 1=1 " +
            " <if test=\"hasSubChannel != null and hasSubChannel == true\" >  AND sub_count >0 </if>" +
            " <if test=\"hasSubChannel != null and hasSubChannel == false\" >  AND sub_count =0 </if>" +
            " ORDER BY channel_id ASC" +
            " </script>"})
    @ResultMap("deviceChannel")
    List<DeviceChannel> queryChannelsByDeviceId(@Param("deviceId")String deviceId, @Param("parentChannelId")String parentChannelId, @Param("query")String query, @Param("hasSubChannel")Boolean hasSubChannel, @Param("online")Boolean online);

    @Results(id="deviceChannel",value={
    		 @Result(property = "channelId", column = "channel_id"),@Result(property = "deviceId", column = "device_id"),
    		 @Result(property = "name", column = "name"),@Result(property = "manufacture", column = "manufacture"),
    		 @Result(property = "model", column = "model"),@Result(property = "owner", column = "owner"),
    		 @Result(property = "civilCode", column = "civil_code"),@Result(property = "block", column = "block"),
    		 @Result(property = "address", column = "address"),@Result(property = "parental", column = "parental"),
    		 @Result(property = "parentId", column = "parent_id"),@Result(property = "safetyWay", column = "safety_way"),
    		 @Result(property = "registerWay", column = "register_way"),@Result(property = "certNum", column = "cert_num"),
    		 @Result(property = "certifiable", column = "certifiable"),@Result(property = "errCode", column = "err_code"),
    		 @Result(property = "endTime", column = "end_time"),@Result(property = "secrecy", column = "secrecy"),
    		 @Result(property = "ipAddress", column = "ip_address"),@Result(property = "port", column = "port"),
    		 @Result(property = "password", column = "password"),@Result(property = "PTZType", column = "ptz_type"),
    		 @Result(property = "status", column = "status"),@Result(property = "createTime", column = "create_time"),
    		 @Result(property = "updateTime", column = "update_time"),@Result(property = "longitude", column = "longitude"),
    		 @Result(property = "latitude", column = "latitude"),@Result(property = "streamId", column = "stream_id"),
    		 @Result(property = "hasAudio", column = "has_audio"),@Result(property = "subCount", column = "subCount"),
    		
    })
    @Select("SELECT * FROM device_channel WHERE device_id=#{deviceId} AND channel_id=#{channelId}")
    DeviceChannel queryChannel(@Param("deviceId")String deviceId, @Param("channelId")String channelId);

    @Delete("DELETE FROM device_channel WHERE device_id=#{deviceId}")
    int cleanChannelsByDeviceId(String deviceId);

    @Update(value = {"UPDATE device_channel SET stream_id=null WHERE device_id=#{deviceId} AND channel_id=#{channelId}"})
    void stopPlay(@Param("deviceId")String deviceId, @Param("channelId")String channelId);

    @Update(value = {"UPDATE device_channel SET stream_id=#{streamId} WHERE device_id=#{deviceId} AND channel_id=#{channelId}"})
    void startPlay(@Param("deviceId")String deviceId, @Param("channelId")String channelId, @Param("streamId")String streamId);


    @Select(value = {" <script>" +
            "SELECT * FROM ( "+
                " SELECT dc.channel_id, dc.device_id, dc.name, de.manufacturer, de.hostAddress, " +
                "(SELECT count(0) FROM device_channel WHERE parent_id=dc.channel_id) as subCount, " +
                "(SELECT pc.platform_id FROM platform_gb_channel pc WHERE pc.device_id=dc.device_id AND pc.channel_id = dc.channel_id ) as platformId " +
                "FROM device_channel dc " +
                "LEFT JOIN device de ON dc.device_id = de.deviceId " +
                " WHERE 1=1 " +
                " <if test=\"query != null\"> AND (dc.channel_id LIKE '%${query}%' OR dc.name LIKE '%${query}%' OR dc.name LIKE '%${query}%')</if> " +
                " <if test=\"online != null and online == true\" > AND dc.status=1</if> " +
                " <if test=\"online != null and online == false\" > AND dc.status=0</if> " +
            ") dcr" +
            " WHERE 1=1 " +
            " <if test=\"hasSubChannel != null and hasSubChannel == true\" >  AND sub_count >0</if> " +
            " <if test=\"hasSubChannel != null and hasSubChannel == false\" >  AND sub_count=0</if> " +
            " <if test=\"platformId != null and inPlatform == true \" >  AND platform_id='${platformId}'</if> " +
            " <if test=\"platformId != null and inPlatform == false \" >  AND (platform_id != '${platformId}' OR platform_id = NULL )  </if> " +
            " ORDER BY device_id, channel_id ASC" +
            " </script>"})
    @Results(id="channelReduce",value={
   		 @Result(property = "channelId", column = "channel_id"),@Result(property = "deviceId", column = "device_id"),
   		 @Result(property = "name", column = "name"),@Result(property = "manufacture", column = "manufacture"),
   		 @Result(property = "subCount", column = "subCount"),@Result(property = "hostAddress", column = "host_address"),
   		 @Result(property = "platformId", column = "platform_id"),
   		
   })
    List<ChannelReduce> queryChannelListInAll(@Param("query")String query, @Param("online")Boolean online, @Param("hasSubChannel")Boolean hasSubChannel, @Param("platformId")String platformId, @Param("inPlatform")Boolean inPlatform);
}
